package com.xy.shop.common.utils;


import org.apache.commons.lang3.StringUtils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;

public class DateUtil {
	// 长日期格式
	public static String TIME_FORMAT = "yyyy-MM-dd HH:mm";


	public static String TIME_FORMAT_MM = "yyyy-MM-dd HH:mm:ss";
	
	public static final Date getNowDate() {
			return new Date(System.currentTimeMillis());
      }
	/**
	 * 将日期格式的字符串转换为长整型
	 * 
	 * @param date
	 * @return
	 */
	public static long convert2long(String date) {
		try {
			String format = DateUtil.TIME_FORMAT;
			if (StringUtils.isNotBlank(date)) {
				SimpleDateFormat sf = new SimpleDateFormat(format);
				return sf.parse(date).getTime();
			}
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return 0l;
	}
	
	public static Integer convertToInt(String date) {
		String dateStr="";
		try {
			String format = DateUtil.TIME_FORMAT;
			if (StringUtils.isNotBlank(date)) {
				SimpleDateFormat sf = new SimpleDateFormat(format);
				long l= sf.parse(date).getTime();
				dateStr=String.valueOf(l);
				if(null!=dateStr&&dateStr.length()>=10){
					dateStr=dateStr.substring(0,10);
				}
			}
		} catch (ParseException e) {

			e.printStackTrace();
		}
		int dateNum=0;
		if(null!=dateStr){
			dateNum=Integer.parseInt(dateStr);
		}
		
		return dateNum;
	}

	public static String getDateStr() {
		SimpleDateFormat df = new SimpleDateFormat("yyyyMMdd");// 设置日期格式
		String str = df.format(new Date());// new Date()为获取当前系统时间
		return str;
	}

	public static Integer convertToInt(String date,String format) {

		String dateStr="";
		try {
			if (StringUtils.isNotBlank(date)) {
				SimpleDateFormat sf = new SimpleDateFormat(format);
				long l= sf.parse(date).getTime();
				dateStr=String.valueOf(l);
				if(null!=dateStr&&dateStr.length()>=10){
					dateStr=dateStr.substring(0,10);
				}
			}
		} catch (ParseException e) {
			e.printStackTrace();
		}
		int dateNum=0;
		if(null!=dateStr){
			dateNum=Integer.parseInt(dateStr);
		}
		return dateNum;
	}

	public static Integer convertToInt(Date date) {
		String dateStr;
		long l = date.getTime();
		dateStr = String.valueOf(l);
		if (dateStr.length() >= 10)
			dateStr = dateStr.substring(0, 10);
		return Integer.parseInt(dateStr);
	}
	
	public static String intToDateStr(String intDateTime,String dateFormat) {
		String dateStr = "";
		try
		{
			if (StringUtils.isNotBlank(intDateTime)) {
				Date date = new Date(Integer.valueOf(intDateTime)*1000L);
				SimpleDateFormat sf = new SimpleDateFormat(dateFormat);
				dateStr = sf.format(date);
			}
		} catch (NumberFormatException e)
		{
			e.printStackTrace();
		}
		return dateStr;
	}
	
	public static String fromatDateToStr(Date date,String dateFormat) {
		String dateStr = "";
		try
		{
			if (date != null) {
				SimpleDateFormat sf = new SimpleDateFormat(dateFormat);
				dateStr = sf.format(date);
			}
		} catch (NumberFormatException e)
		{
			e.printStackTrace();
		}
		return dateStr;
	}
	
	
	/**
	 * 把当前的时间转int
	 * @return
	 */
	public static Integer currentTimeToInt(){
		String currentTime=String.valueOf(System.currentTimeMillis());
		String time=currentTime.substring(0,10);
		return Integer.parseInt(time);
	}
	
	

	
	 /** 
	  * 将长整型数字转换为日期格式的字符串 
	  *  
	  * @param time 
	  * @return
	  */  
	 public static String convert2String(long time) {  
	   String format = DateUtil.TIME_FORMAT;
	   if(time>0l) {  
	   if (StringUtils.isBlank(format))  
	    format =  DateUtil.TIME_FORMAT;  
	   SimpleDateFormat sf = new SimpleDateFormat(format);  
	   Date date = new Date(time);  
	   return sf.format(date);  
	  }  
	  return "";  
	 } 
	 

    
    /**
     * 获取参数时间为该年第几周
     * @param dateTime
     * @return
     * @throws ParseException
     */
    public static int getDateTOWeek (String dateTime) throws ParseException{
    	int toWeek=0;
    	SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");  
    	Date date  = format.parse(dateTime);  
    	Calendar calendar = Calendar.getInstance();  
    	calendar.setFirstDayOfWeek(Calendar.MONDAY);  
    	calendar.setTime(date);  
    	toWeek=calendar.get(Calendar.WEEK_OF_YEAR);  
    	return toWeek;
	}
    
    /**
     * 根据参数时间获取该周最后天时间
     * @param dateTime
     * @return z周日
     * @throws ParseException
     */
    public static String getDatalastDay(String dateTime){
    	Calendar cal = Calendar.getInstance();

		try {
			cal.setTime(new SimpleDateFormat("yyyy-MM-dd").parse(dateTime));
		} catch (ParseException e) {
			e.printStackTrace();
		}

		int d = 0;
		if(cal.get(Calendar.DAY_OF_WEEK)==1){
			d = -6;
		}else{
			d = 2-cal.get(Calendar.DAY_OF_WEEK);
		}
		cal.add(Calendar.DAY_OF_WEEK, d);
		//所在周开始日期
		//new SimpleDateFormat("yyyy-MM-dd").format(cal.getTime());
		cal.add(Calendar.DAY_OF_WEEK, 6);
		//所在周结束日期
		dateTime=new SimpleDateFormat("yyyy-MM-dd").format(cal.getTime());
		return dateTime;
    }
    /**
     * 获取参数时间周一日期
     * @param time
     * @return
     * @throws ParseException
     * @author:zyy
     * @time:2017年3月30日
     */
    public static String getDateFilstDay(String time) {

    	Calendar cal = Calendar.getInstance();

    	try {
			cal.setTime(new SimpleDateFormat("yyyy-MM-dd").parse(time));
		} catch (ParseException e) {
			e.printStackTrace();
		}

    	int d = 0;
    	if(cal.get(Calendar.DAY_OF_WEEK)==1){
    		d = -6;
    	}else{
    		d = 2-cal.get(Calendar.DAY_OF_WEEK);
    	}
    	cal.add(Calendar.DAY_OF_WEEK, d);
    	time=new SimpleDateFormat("yyyy-MM-dd").format(cal.getTime());
    	return time;
    }
    /**
     * 获取当前时间
     * @return String 时间 yyyy-MM-dd
     */
    public static String getNewDate(){
    	String date="";
    	SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
    	date=sdf.format(new Date());
    	return date;
    }
    /**
     * 获取当前时间
     * @return String 时间yyyy-MM-dd HH:mm:ss
     */
    public static String getNowDates(){
    	String date="";
    	SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    	date=sdf.format(new Date());
    	return date;
    }
    
    /**
	 * 
	 * @num推迟几天
	 * @return
	 * @throws ParseException
	 */
	public static String getEndDate(String time,long num) throws ParseException{
		
		/*SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
		Calendar lastDate = new GregorianCalendar();
		Date da=sdf.parse(time);
		lastDate.setTime(da);
		lastDate.roll(Calendar.DATE, -num);//日期回滚7天
	    time=sdf.format(lastDate.getTime());*/
		SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
		String returnTime="";
		Calendar cal=Calendar.getInstance();
		cal.setTime(sdf.parse(time));
		long longTime=cal.getTimeInMillis();
		longTime=longTime-(24*60*60*1000)*num;
		returnTime=convert2String(longTime);
		
		return returnTime.substring(0,returnTime.indexOf(" "));
	}
	public static String getStartDate(String time,long num) {

		/*SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
		Calendar lastDate = new GregorianCalendar();
		Date da=sdf.parse(time);
		lastDate.setTime(da);
		lastDate.roll(Calendar.DATE, -num);//日期回滚7天
	    time=sdf.format(lastDate.getTime());*/
		SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
		String returnTime="";
		Calendar cal=Calendar.getInstance();
		try {
			cal.setTime(sdf.parse(time));
		} catch (ParseException e) {
			e.printStackTrace();
		}
		long longTime=cal.getTimeInMillis();
		longTime=longTime+(24*60*60*1000)*num;
		returnTime=convert2String(longTime);

		return returnTime.substring(0,returnTime.indexOf(" "));
	}

	/**
	 * @param null
	 * @Description: 方法说明 beginTime大于endTime true  小于false 等于false 用与时间比较
	 * @Author: zyy
	 * @date: 2018/12/21 11:29
	 */
	public static boolean BJDateSize(String beginTime,String endTime){

		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
		boolean before=false;
		try {
			Date date1 = format.parse(beginTime);
			Date date2 = format.parse(endTime);

			before= date1.before(date2);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return before;
	}
	/**
	 * 根据date后day天数
	 * @param date 如期
	 * @param day 几天
	 * @return
	 * @throws ParseException
	 * @author:zyy
	 * @time:2017年9月6日
	 */
	public static Date setLongTimeDate(Date date,long day) throws ParseException {
		 long time = date.getTime(); // 得到指定日期的毫秒数
		 day = day*24*60*60*1000; // 要加上的天数转换成毫秒数
		 time+=day; // 相加得到新的毫秒数
		 return new Date(time); // 将毫秒数转换成日期
		}
	/**
	 * 获取当前时间的上月时间往后推一个月时间
	 * @return String
	 */
	public static String getPreMonth() {
		Calendar cal = Calendar.getInstance();
		cal.add(cal.MONTH, -1);
		SimpleDateFormat dft = new SimpleDateFormat("yyyy-MM-dd");
		String preMonth = dft.format(cal.getTime());
		return preMonth;
	}
	/**
	 * 获取当前时间加1天
	 * @param date
	 * @return
	 * @author:zyy
	 * @time:2017年3月30日
	 */
	public static String getNextDay(Date date) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.DAY_OF_MONTH, +1);//+1今天的时间加一天
        date = calendar.getTime();
        return sdf.format(date);
    }
	
	  public static String getDatalastWeek(){
	    	
			SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
		    Calendar date=Calendar.getInstance(Locale.CHINA);

		    date.setFirstDayOfWeek(Calendar.MONDAY);//将每周第一天设为星期一，默认是星期天

		    date.add(Calendar.WEEK_OF_MONTH,-1);//周数减一，即上周

		    date.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY);//日子设为星期天
		    
			return sdf.format(date.getTime());
	    }

	public static String getCurrentDayStr(){
		 SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
		String curDate=sdf.format(new Date());
		return curDate;
	}
    
	/**
	* 日期转换成字符串
	* @param date 
	* @return str
	*/
	public static String DateToStr(Date date) {
	  
	   SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	   String str = format.format(date);
	   return str;
	}
	public static String DateToStrX(Date date) {

		SimpleDateFormat format = new SimpleDateFormat("HH:mm:ss");
		String str = format.format(date);
		return str;
	}
	/**
	 * 
	 * @标题: StrToDate 
	 * @描述: 将日期型的字符串转为 带有时分秒格式的date     
	 * 	"2017-11-23" --->Thu Nov 23 00:00:00 CST 2017
	 *
	 * @参数信息
	 *    @param str
	 *    @return
	 *
	 * @返回类型 Date
	 * @开发者 zhengshengfei
	 * @可能抛出异常
	 */
	public static Date StrToDate(String str) {
	   SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	   Date date = null;
	   try {
		if (StringUtils.isNotBlank(str)){
			str+=" 00:00:00";
			date = format.parse(str);
		}
	   } catch (ParseException e) {
	    e.printStackTrace();
	   }
	   return date;
	}
	public static Date StrToDateMM(String str) {
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Date date = null;
		try {
			if (StringUtils.isNotBlank(str)){
				date = format.parse(str);
			}
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return date;
	}
	/** 
     * 获取当前日期是星期几<br> 
     *  
     * @param date 
     * @return 当前日期是星期几 
     */  
    public static int getWeekOfDate(Date date) {  
       // String[] weekDays = { "星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六" };  
        Calendar cal = Calendar.getInstance();  
        cal.setTime(date);  
        int w = cal.get(Calendar.DAY_OF_WEEK) - 1;  
        System.out.println(w);
      /*  if (w < 0)  
            w = 0;*/  
        return w;  
    }  
    
    
    public static String getOneLastMonth(Date date){
    	
    	Calendar cal = Calendar.getInstance();  
    	cal.setTime(date);
        cal.add(cal.MONTH, 1);  
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String lastMonth = format.format(cal.getTime());  
        return lastMonth; 
    }
	
    
    public  static  int  dayForWeek(Date pTime){  
    	 Calendar c = Calendar.getInstance();  
    	 c.setTime(pTime);  
    	 int  dayForWeek = 0 ;  
    	 if (c.get(Calendar.DAY_OF_WEEK) == 1 ){  
    	  dayForWeek = 7 ;  
    	 }else {  
    	  dayForWeek = c.get(Calendar.DAY_OF_WEEK) - 1 ;  
    	 }  
    	 return  dayForWeek;  
    	}  
    
    public static String intToDateString(String time){
    	 SimpleDateFormat simpleDateFormat =  new SimpleDateFormat(TIME_FORMAT);  
    	 String res;
         long lt = new Long(time);
         Date date = new Date(lt*1000);
         res = simpleDateFormat.format(date);
         return res;
    }


	/**
	 * 获取两个字符串日期时间戳之间相差天数（时间戳单位为秒）
	 * @author Yangy
	 * @param startTime
	 * @param endTime
	 * @return
	 */
	public static int compareDateOfDays(String startTime,String endTime){
    	//SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    	int days;

		Date startDate = new Date(Long.valueOf(startTime)*1000);
		Date endDate = new Date(Long.valueOf(endTime)*1000);

		//System.out.println(sdf.format(startDate)+","+sdf.format(endDate));

		Calendar calendar = Calendar.getInstance();

		calendar.setTime(startDate);
		int startDays = calendar.get(Calendar.DAY_OF_YEAR);

		calendar.setTime(endDate);
		int endDays = calendar.get(Calendar.DAY_OF_YEAR);

		days = endDays - startDays +1;

		return days;
	}

	public static int compareDateStrOfDays(String startTime,String endTime) throws ParseException {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

		Date startDate = sdf.parse(startTime);
		Date endDate = sdf.parse(endTime);

		long startSeconds = startDate.getTime();
		long endSeconds = endDate.getTime();
		int days = new Long((endSeconds-startSeconds)/(24*3600*1000)).intValue();

		return days;
	}
	
	/**
	 * @Author Yangy
	 * @Description 计算两个日期之间相隔的天数
	 * @Date 2019/3/15 10:13 
	 * @Param [startTime, endTime]
	 * @return int
	 **/
	public static int compareDateOfDays(Date startTime,Date endTime){
		long startSeconds = startTime.getTime();
		long endSeconds = endTime.getTime();
		int days = new Long((endSeconds-startSeconds)/(24*3600*1000)).intValue();
		return days;
	}

	public static String getEndDateParam(String time,long num) throws ParseException{

		/*SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
		Calendar lastDate = new GregorianCalendar();
		Date da=sdf.parse(time);
		lastDate.setTime(da);
		lastDate.roll(Calendar.DATE, -num);//日期回滚7天
	    time=sdf.format(lastDate.getTime());*/
		SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
		String returnTime="";
		Calendar cal=Calendar.getInstance();
		cal.setTime(sdf.parse(time));
		long longTime=cal.getTimeInMillis();
		longTime=longTime-(24*60*60*1000)*num;
		returnTime=convert2String(longTime);

		return returnTime ;
	}

	public static Date getStringToDate(String date){
		SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
		try {
			return sdf.parse(date);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return null;
	}

	public static Date todayFirstDate() {
		Calendar calendar = Calendar.getInstance();
		calendar.set(Calendar.HOUR_OF_DAY, 0);
		calendar.set(Calendar.MINUTE, 0);
		calendar.set(Calendar.SECOND, 0);
		calendar.set(Calendar.MILLISECOND, 0);
		return calendar.getTime();
	}

	public static Date todayLastDate() {
		Calendar calendar = Calendar.getInstance();
		calendar.set(Calendar.HOUR_OF_DAY, 23);
		calendar.set(Calendar.MINUTE, 59);
		calendar.set(Calendar.SECOND, 59);
		calendar.set(Calendar.MILLISECOND, 999);
		return calendar.getTime();
	}

    public static void main(String[] args) throws Exception {/*
		//System.out.println(DateUtil.convert2long("2017-02-13 00:00"));
		Long l=1490152553468l;
		
		System.out.println(convertToInt("2017-03-30 12:02:01"));
		//System.out.println(getNextDay(new Date()));

		//所在周结束日期
		//System.out.println(getDateFilstDay("2017-03-30"));
		System.out.println(getEndDate("2017-05-06",6));
	*/
    /*	SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    	Date date = new Date(1469103031*1000L);
    	//convertToInt.
    	int d=DateUtil.convertToInt(DateUtil.getCurrentDayStr(),"yyyy-MM-dd HH");*/
    	//System.out.println("d===::"+intToDateString("1522140125"));
		//{"svflag":1,"data":{"reward":0,"descr":"公司6周年庆代理商活动","cardRecord":0,"closeTime":1528364570,"open_time":1528364568.000000,"startTime":1528992000,"endTime":1529769600,"title":"6周年狂欢派豪礼","type":45,"status":0}}
//    	int s=currentTimeToInt()+1*60*60;
//		System.out.println("相差天数:"+DateUtils.TimeStampToDate(currentTimeToInt()+1*60*60+""));
//		System.out.println(currentTimeToInt()+1*60*60);
//		System.out.println(BJDateSize("2018-07-27","2018-07-28"));
		Calendar calendar = Calendar.getInstance();
		calendar.set(2019,02,12,00,00,00);
		System.out.println("开始时间为："+StrToDate("2019-03-12 15:00:00"));
		System.out.println("相差天数："+compareDateOfDays(StrToDate("2019-03-12 00:00:00"),new Date()));
    }
    
	
}

